package edu.ptit.jad.model;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class ContentSimilarity {

	private int id;
	private double similariry;

	public ContentSimilarity(int id, double similarity) {
		this.setId(id);
		this.setSimilariry(similarity);
	}

	public static void sort(List<ContentSimilarity> contentSimilaritys) {

		Collections.sort(contentSimilaritys,
				new Comparator<ContentSimilarity>() {
					public int compare(ContentSimilarity f1,
							ContentSimilarity f2) {
						int result = 0;
						if (f1.getSimilarity() < f2.getSimilarity()) {
							result = 1; // reverse order
						} else if (f1.getSimilarity() > f2.getSimilarity()) {
							result = -1;
						} else {
							result = 0;
						}
						return result;
					}
				});
	}

	public static ContentSimilarity[] getTopN(
			List<ContentSimilarity> contentSimilaritys, int topN) {

		// sort friends based on itemAgreement
		ContentSimilarity.sort(contentSimilaritys);

		// select top N friends
		List<ContentSimilarity> topFriends = new ArrayList<ContentSimilarity>();
		for (ContentSimilarity f : contentSimilaritys) {
			
			topFriends.add(f);
			if (topFriends.size() >= topN) {
				// have enough friends.
				break;
			}

		}

		return topFriends.toArray(new ContentSimilarity[topFriends.size()]);
	}

	public double getSimilarity() {
		return similariry;
	}

	public void setSimilariry(double similariry) {
		this.similariry = similariry;
	}

	public int getId() {
		return id;
	}

	void setId(int id) {
		this.id = id;
	}
}
